<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bar extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
	}

	public function index(){
		$this->load->model('barang','',true);
		$data['barang'] = $this->barang->get_all();
		$this->load->view('header');
		$this->load->view('menu');
		$this->load->view('barang',$data);
		$this->load->view('footer');
	}

	public function insert(){
		$data = array(
				'id_barang' 			=> $this->input->post('id_barang'),
				'nama' 					=> $this->input->post('nama'),
				'keterangan' 			=> $this->input->post('keterangan'),
				'harga_tunai'			=> $this->input->post('harga_tunai'),
				'harga_kredit_satuan'	=> $this->input->post('harga_kredit_satuan'),
				'harga_kredit_grosir'	=> $this->input->post('harga_kredit_grosir'),
				'angsuran'				=> $this->input->post('angsuran')
			);
		$this->load->model('barang','',true);
		$this->form_validation->set_rules('id_barang', 'id_barang', 'trim|required|is_unique[barang.id_barang]');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('barang','',true);
			$data['barang'] = $this->barang->get_all();
			$this->load->view('header');
			$this->load->view('menu');
			$this->load->view('barang',$data);
			$this->load->view('footer');
		} else {
			$this->barang->insert($data);
			redirect('bar');
		}	
	}

	public function delete($data){
		$this->load->model('barang','',true);
		$this->barang->delete($data);
		redirect('bar');
	}

	public function edit($id){
		$this->load->model('barang','',true);
		$data['detail'] = $this->barang->get_by_id($id);
		$data['barang'] = $this->barang->get_all();
		$this->load->view('header');
		$this->load->view('menu');
		$this->load->view('barang',$data);
		$this->load->view('footer');
	}

	public function update(){
		$data = array(
				'id_barang' 			=> $this->input->post('id_barang'),
				'nama' 					=> $this->input->post('nama'),
				'keterangan' 			=> $this->input->post('keterangan'),
				'harga_tunai'			=> $this->input->post('harga_tunai'),
				'harga_kredit_satuan'	=> $this->input->post('harga_kredit_satuan'),
				'harga_kredit_grosir'	=> $this->input->post('harga_kredit_grosir'),
				'angsuran'				=> $this->input->post('angsuran')
			);
		$id = $this->input->post('id_barang');
		$this->load->model('barang','',true);
		$this->form_validation->set_rules('id_barang', 'id_barang', 'trim|required');
		if ($this->form_validation->run() == FALSE) {
			$menu['error'] = true;
			$this->load->model('barang','',true);
			$data['barang'] = $this->barang->get_all();
			$this->load->view('header');
			$this->load->view('menu');
			$this->load->view('barang',$data);
			$this->load->view('footer');
		} else {
			$this->barang->update($id,$data);
			redirect('bar');
		}
		
	}
}
